<?
include_once 'CoreModel.php';
Class Forummodel extends CoreModel{
	private $table;
	public function __construct(){
		$this->table = "forum";
		parent::__construct();
	}
	
	public function createData(){
		$data['title'] = "";
		$data['createOn'] = date("Y-m-d H:i:s");
		$data['updateOn'] = date("Y-m-d H:i:s");
		$data['parent'] = 0;
		$data['isMain'] = "Y";
		$data['postby'] = $this->session->userdata("sale_no");
		$data['postname'] = $this->session->userdata("sale_name");
		$data['lastPostID'] = $this->session->userdata("sale_no");
		$data['lastPostName'] = $this->session->userdata("sale_name");
		$this->db->insert($this->table,$data);
		return $this->db->insert_id();
		
	}
	public function updateForum($data){
		$id = $data['fID'];
		unset($data['fID']);
		
		$this->db->where("fID",$id);
		$this->db->update($this->table,$data);
	}
	public function getAForum($id,$userID=''){
		$this->db->select("*");
		$this->db->where("fID",$id);
		if($userID!=""){
			//$this->db->where("postby",$userID);	
		}
		$query = $this->db->get($this->table);
		if($query->num_rows() > 0 ){
			return $query->row(0);
		}else{
			return null;
		}
	}
	public function deleteForum($id,$isMain){
		if($isMain=="Y"){
			$this->db->where("parent",$id);
			$this->db->set("status","Delete");
			$this->db->update("forum");
		}
		$this->db->where("fID",$id);
		$this->db->set("status","Delete");
		$this->db->update("forum");
		
	}
	public function replyForum($data){
			
		
		$data['isMain'] = 'N';
		$data['Status'] = 'Active';
		$data['postby'] = $this->session->userdata("sale_no");
		$data['postname'] = $this->session->userdata("sale_name");
		$data['lastPostID'] = $this->session->userdata("sale_no");
		$data['lastPostName'] = $this->session->userdata("sale_name");
		$data['updateOn'] = date("Y-m-d H:i:s");
		$data['createOn'] = date("Y-m-d H:i:s");
		$this->db->trans_start();
		// insert reply
		$this->db->insert($this->table,$data);
		
		// update num of comment
		$this->db->set('numOfComment', '`numOfComment`+ 1', FALSE);
		$this->db->set("updateOn",date("Y-m-d H:i:s"));
		$this->db->set("lastPostID",$this->session->userdata("sale_no"));
		$this->db->set("lastPostName",$this->session->userdata("sale_name"));
		$this->db->where("fID",$data['parent']);
		$this->db->update($this->table);
		
		$this->db->trans_complete();
	}
	public function updateView($id){
		$this->db->where("fID",$id);
		$this->db->set('numOfView', '`numOfView`+ 1', FALSE);
		$this->db->update($this->table);
	}
	public function getAForumAndDetail($id){
		$this->db->select("*");
		$this->db->where("fID",$id);
		$this->db->where("status","Active");
		$this->db->where("isMain","Y");
		$query = $this->db->get($this->table);
		if($query->num_rows() > 0 ){
			$dataResult = $query->row(0);
			$this->db->select("*");
			$this->db->where("parent",$id);
			$this->db->where("status","Active");
			$this->db->order_by("createOn","ASC");
			$query = $this->db->get($this->table);
			if($query->num_rows() > 0 ){
				$dataResult->ListDetail = $query->result();
			}
			return $dataResult;
			
		}else{
			return null;
		}
	}
	public function getForumCount(){
		
		$this->db->where("parent","0");
		$this->db->where("status","Active");
		$this->db->where("isMain","Y");
		$this->db->order_by("isPin","DESC");
		$this->db->order_by("updateOn","Desc");
		$this->db->from($this->table);
		
		return $this->db->count_all_results();
	}
	public function getForum($start,$limit){
		
		$sql = "select * from forum where parent = '0' and status = 'Active' and isMain = 'Y' order by isPin DESC, forumSeq ASC ,UpdateOn DESC LIMIT $start,$limit";
		$query = $this->db->query($sql);
		
		if($query->num_rows() > 0 ){
			return $query->result();
		}else{
			return null;
		}
	}
	/*
	
	public function getCustomer($saleID='',$queryString='',$start='',$limit=''){
		$this->db->select("*");
		if($saleID!=''){
			$this->db->where("SALES_NO",$saleID);	
		}
		$this->db->limit($limit,$start);
		$query = $this->db->get($this->table);
		
		if($query->num_rows() > 0 ){
			return $query->result();
		}else{
			return null;
		}
	}
	
	public function updateLoaction($data){
		$id = $data['KUNNR'];
		unset($data['KUNNR']);
		$this->db->where("KUNNR",$id);
		$this->db->update($this->table,$data);
	}
	
	*/
}
?>